package persistencia;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Vector;

import dominio.Autor;
import dominio.Persona;
import dominio.Revisor;

public class PersonaDAO {

	public static void insert(Persona per) throws Exception {
		String SQL="Insert into Persona values (?, ?, ?, ?, ?, ?, ?)";
		Conexion bd=null;
		Exception ex=null;
		PreparedStatement p=null;
		try {
			bd=Broker.get().getBD();
			p=bd.prepareStatement(SQL);
			p.setString(1, per.getDni());
			p.setString(2, per.getNombre());
			p.setString(3, per.getApellido());
			p.setString(4, per.getAfiliacion());
			p.setString(5, per.getEmail());
			p.setString(6, per.getPassword());
			p.setString(7, per.getRol());
			p.executeUpdate();
		}
		catch (Exception e) {
			ex=e;
		}
		finally {
			bd.close();
			if (ex!=null)
				throw ex;
		}
	}
	
	public static void delete(Persona per) throws Exception {
		String SQL="Delete from Persona where dni=?";
		Conexion bd=null;
		Exception ex=null;
		PreparedStatement p=null;
		try {
			bd=Broker.get().getBD();
			p=bd.prepareStatement(SQL);
			p.setString(1, per.getDni());
			p.executeUpdate();
		}
		catch (Exception e) {
			ex=e;
		}
		finally {
			bd.close();
			if (ex!=null)
				throw ex;
		}	
	}
	
	public static String getRol(String dni) throws Exception{
		String SQL="select rol from Persona where dni=?";
		Conexion bd=null;
		Exception ex=null;
		PreparedStatement p=null;
		String rol = "";
		try {
				bd = Broker.get().getBD();
				p = bd.prepareStatement(SQL);
				p.setString(1, dni);
				ResultSet r = p.executeQuery();
				if (r.next()) {
					rol = r.getString("rol");
				} else {
					throw new Exception("La persona con NIF " + dni + " no existe");
				}
		} catch (Exception e) {
			ex = e;
	}
		finally {
			bd.close();
			if (ex!=null)
				throw ex;
		}
		return rol;
	}
	
	public static void update(Persona per) throws Exception {
		String SQL="UPDATE Persona SET nombre=?, apellido=?, afiliacion=?, email=?, pass=?, rol=? where dni=?;";
		Conexion bd=null;
		Exception ex=null;
		PreparedStatement p=null;
		try {
			bd=Broker.get().getBD();
			p=bd.prepareStatement(SQL);
			p.setString(7, per.getDni());
			p.setString(1, per.getNombre());
			p.setString(2, per.getApellido());
			p.setString(3, per.getAfiliacion());
			p.setString(4, per.getEmail());
			p.setString(5, per.getPassword());
			p.setString(6, per.getRol());
			p.executeUpdate();
		}
		catch (Exception e) {
			ex=e;
		}
		finally {
			bd.close();
			if (ex!=null)
				throw ex;
		}
	}
	
	
	public static Persona validar(String dni, String password) throws Exception{
		String SQL="select pass, rol from Persona where dni=?";
		Persona result = null;
		Conexion bd=null;
		Exception ex=null;
		PreparedStatement p=null;
		String pass = "";
		String rol = "";
		try {
				bd = Broker.get().getBD();
				p = bd.prepareStatement(SQL);
				p.setString(1, dni);
				ResultSet r = p.executeQuery();
				if (r.next()) {
					pass = r.getString("pass");
					rol = r.getString("rol");
				} else {
					throw new Exception("La persona con NIF " + dni + " no existe");
				}
				if(pass.equals(password)){
					if(rol.equalsIgnoreCase("editor")){
						result = EditorDAO.newEditor(dni);
					}else if(rol.equalsIgnoreCase("revisor")){
						result = RevisorDAO.newRevisor(dni);
					} else if(rol.equalsIgnoreCase("autor")){
						result = AutorDAO.newAutor(dni);
					}
				}
		} catch (Exception e) {
			ex = e;
	}
		finally {
			bd.close();
			if (ex!=null)
				throw ex;
		}
		return result;
	}
}
